package iosr11.ui.data;

import iosr11.Service.ServiceLocatorImpl;
import iosr11.Service.StudentService;
import iosr11.Service.MarkService;
import iosr11.data.Student;
import iosr11.data.Mark;
import iosr11.data.MarkKind;
import iosr11.data.User;
import iosr11.ui.common.CurrentUserBean;

import java.util.Map;
import java.util.logging.Logger;

import javax.faces.context.FacesContext;

public class StudentBean {
	private StudentService service;
	private MarkService service_mark;
	private Logger logger = Logger.getLogger("iosr.ui.data");
	private Student student;
	private Mark newMark;
	private String originalEmail;
	private CurrentUserBean currentUser;
	
	public StudentBean(){
		if(this.student==null) {
			this.service = (StudentService)ServiceLocatorImpl.getInstance().getService(Student.class);
			if(this.service == null){
				logger.severe("Could not obtain reference to StudentService");
				return;
			}
			Map sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
			if(sessionMap.containsKey(SessionMapKeys.SelectedStudentEmail.name())){ // editing mode
				this.originalEmail = (String)sessionMap.get(SessionMapKeys.SelectedStudentEmail.name());			
				this.student = service.getStudent(this.originalEmail);
				
				if(this.student==null){
					logger.severe("Could not obtain student from StudentService");
				}
			}else{	// adding new student mode
				this.student = new Student();
				this.student.setUser(new User());
			}
		}
	}
	
	public Student getStudent(){
		return this.student;
	}
	
	public float getNewMark(){
		if(this.newMark == null) {
			return 0;
		}
		return this.newMark.getGrade();
	}
	
	public void setNewMark(float grade){
		if(this.newMark == null) {
			this.newMark = new Mark();
		}
		this.newMark.setKind(MarkKind.PROJECT);
		this.newMark.setGrade(grade);
	}
	
	public boolean getEditMode(){
		return (this.student.getId()!=null);
	}
	
	public String saveMark(){
		this.service_mark = (MarkService)ServiceLocatorImpl.getInstance().getService(Mark.class);
		if(this.service_mark == null){
			logger.severe("Could not obtain reference to MarkService");
			return "failure";
		}
		if(this.service_mark.createMark(this.student, currentUser.getTeacher(), this.newMark)){
			return "success";
		}else{
			return "failure";
		}
	}
	public String updateStudent(){
		if(this.service.updateStudent(this.student)){
			return "success";
		}else{
			return "failure";
		}
	}
	
	public String addStudent(){
		if(this.service.createStudent(this.student.getName(), this.student.getEmail(), this.student.getIndexNumber())){
			return "success";
		}else{
			return "failure";
		}
	}

	public String delete(){
		if(this.originalEmail!=null && this.service.deleteStudent(this.originalEmail)){
			return "success";
		}else{
			return "failure";
		}
	}
	public CurrentUserBean getCurrentUser() {
		return currentUser;
	}
	
	public void setCurrentUser(CurrentUserBean currentUser) {
		this.currentUser = currentUser;
	}
}
